{{!
  Copyright IBM Corp. 2016, 2025
  SPDX-License-Identifier: BUSL-1.1
}}

<Hds::Dropdown @enableCollisionDetection={{true}} @width="210px" data-test-user-menu as |D|>
  <D.ToggleIcon @size="medium" @icon="user" @text="User menu" @hasChevron={{false}} data-test-button="user-menu-trigger" />
  <D.Title @text={{capitalize this.auth.authData.displayName}} data-test-user-menu-item="title" />
  {{#if this.auth.allowExpiration}}
    <D.Header data-test-user-menu-item="token alert">
      <Hds::Alert class="has-top-margin-s" @type="compact" @icon="alert-triangle-fill" @color="warning" as |A|>
        <A.Description>We've stopped auto-renewing your token due to inactivity. It will expire on
          {{date-format this.auth.tokenExpirationDate "MMMM do yyyy, h:mm:ss a"}}.
        </A.Description>
      </Hds::Alert>
    </D.Header>
  {{/if}}

  {{#if this.hasEntityId}}
    <D.Interactive @route="vault.cluster.mfa-setup" data-test-user-menu-item="mfa">
      Multi-factor authentication
    </D.Interactive>
  {{/if}}

  {{#if this.isUserpass}}
    <D.Interactive @route="vault.cluster.access.reset-password" data-test-user-menu-item="reset-password">Reset password</D.Interactive>
  {{/if}}

  <D.Generic id="container">
    <Hds::Copy::Button
      @text="Copy token"
      @textToCopy={{this.auth.currentToken}}
      @isFullWidth={{true}}
      @container="#container"
      class="in-dropdown link is-flex-start is-shadowless"
      @isInline={{true}}
      @onSuccess={{(fn (set-flash-message "Token copied!"))}}
      @onError={{(fn (set-flash-message "Clipboard copy failed. The Clipboard API requires a secure context." "danger"))}}
      data-test-copy-button
    />
  </D.Generic>

  {{#if (is-before (now interval=1000) this.auth.tokenExpirationDate)}}
    {{#if this.auth.authData.renewable}}
      <D.Interactive {{on "click" this.renewToken}} @isLoading={{this.isRenewing}} data-test-user-menu-item="renew token">
        Renew token
      </D.Interactive>
    {{/if}}

    <D.Interactive
      {{on "click" (fn (mut this.showRevokeModal) true)}}
      @color="critical"
      data-test-user-menu-item="revoke token"
    >
      Revoke token
    </D.Interactive>

  {{/if}}
  <D.Interactive @route="vault.cluster.logout" @model={{this.currentCluster.cluster.name}} data-test-user-menu-item="logout">
    Log out
  </D.Interactive>
</Hds::Dropdown>

{{#if this.showRevokeModal}}
  <ConfirmModal
    @color="critical"
    @confirmMessage="You will not be able to log in again with this token."
    @confirmTitle="Revoke {{get this.auth 'authData.displayName'}}?"
    @onClose={{fn (mut this.showRevokeModal) false}}
    @onConfirm={{this.revokeToken}}
  />
{{/if}}